/**
 * @file    drv_wdg.c
 * @author  yhy
 * @brief   芯必达IM94x系列芯片, wdg抽象层
 * @version 1.00
 * @date    2025-07-09
 *
 * @copyright Copyright (c) 2025
 *
 * @details
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-09     yhy          the first version V1.00
 *
 */
#include "drv_wdg.h"

#include "wdg_drv.h"

#define LSI_CLK_FREQ 32000U /* 32kHz */
#define WDG_INSTANCE 0U

wdg_config_t wdg_base_config = {
    .clockSource      = WDG_CLK_SRC_LSI,
    .updateEnable     = true,
    .intEnable        = true,
    .windowModeEnable = false,
    .prescalerEnable  = false,
    .windowValue      = 0U,
};

/**
 * @brief 初始化看门狗, 在指定时间内没有喂狗, 就会复位
 *
 * @param timeout_ms    看门狗超时时间, 单位ms max: 2000ms
 * @return result_e     初始化结果, RESULT_STATUS_OK表示初始化成功, 其他值表示失败
 */
result_e snf_drv_wdg_init(uint16_t timeout_ms)
{
    uint16_t timeout_value = timeout_ms * LSI_CLK_FREQ / 1000;

    wdg_base_config.timeoutValue = timeout_value - 1;

    errcode_t ret = WDG_Init(WDG_INSTANCE, &wdg_base_config);
    if (ret != ERR_SUCCESS) {
        return RESULT_STATUS_WDG_INIT_ERROR;
    }

    return RESULT_STATUS_OK;
}

/**
 * @brief 喂狗操作
 *
 */
void snf_drv_wdg_refresh(void) { WDG_Refresh(WDG_INSTANCE); }
